package android.Spider;

import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Spinner;

public class UserProfileDetails extends UserProfile
{
	@Override
	protected void SetContentView()
	{
		setContentView(R.layout.profiledetails);
        ((Gallery)findViewById(R.id.galleryAvatars)).setAdapter(new ImageAdapter(this));
        
		this.setResult(0);
	}

	@Override
	protected void SetCurrentSettings()
	{
		if (m_dlgProgress == null && m_usrProfile != null && SpiderListener.UserName.length() > 0)
		{
			m_usrProfile.fromBundle(getIntent().getExtras());

			// Set all interests that user already configured
			int nInterests = m_usrProfile.m_nInterests;
			for (int i = 0; i < arrInterests.length; ++i)
				((CheckBox)findViewById(arrInterests[i])).setChecked((nInterests & ProfileUser.arrInterestsFlags[i]) != 0);
			
			// Define occupations
    		Spinner spinner = (Spinner)findViewById(R.id.ProfileDetailsOcupation);
    		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.occupations, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter);
            spinner.setSelection(m_usrProfile.m_nOccupation == ProfileUser.Other ? 6 : m_usrProfile.m_nOccupation);
            
    		Gallery g = (Gallery)findViewById(R.id.galleryAvatars);
    		g.setSelection(m_usrProfile.m_nAvatar);

			// Define countries
    		spinner = (Spinner)findViewById(R.id.ProfileDetailsCountry);
    		adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter);
            spinner.setSelection(m_usrProfile.m_nCountry);

            if (m_usrProfile.m_bMale != null)
            {
            	if (m_usrProfile.m_bMale)
            		((RadioButton)findViewById(R.id.ProfileDetalisRadioButtonMale)).setChecked(true);
            	else
            		((RadioButton)findViewById(R.id.ProfileDetalisRadioButtonFemale)).setChecked(true);
            }
            
            ((EditText)findViewById(R.id.ProfileDetailsTown)).setText(m_usrProfile.m_strTown);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    menu.add(0, 0, "Submit", R.drawable.submitsend);
	    menu.add(0, 2, "Discard", R.drawable.discard);
	    return true;
	}

	@Override
	public boolean onOptionsItemSelected(Menu.Item item)
	{
		if (item.getId() == 0)
		{
			// Fetch data from interests
			int nInterests = 0;
			for (int i = 0; i < arrInterests.length; ++i)
			{
				CheckBox chk = (CheckBox)findViewById(arrInterests[i]);
				if (chk.isChecked())
					nInterests += ProfileUser.arrInterestsFlags[i];
			}
			m_usrProfile.m_nInterests = nInterests;
			
			// Fetch occupation
    		Spinner spinner = (Spinner)findViewById(R.id.ProfileDetailsOcupation);
    		m_usrProfile.m_nOccupation = spinner.getSelectedItemPosition();
			
    		// Fetch gender
    		if (((RadioButton)findViewById(R.id.ProfileDetalisRadioButtonMale)).isChecked())
    			m_usrProfile.m_bMale = new Boolean(true);
    		else if (((RadioButton)findViewById(R.id.ProfileDetalisRadioButtonFemale)).isChecked())
    			m_usrProfile.m_bMale = new Boolean(false);
        	else
        		m_usrProfile.m_bMale = null;

    		// Fetch avatar
    		Gallery g = (Gallery)findViewById(R.id.galleryAvatars);
    		m_usrProfile.m_nAvatar = g.getSelectedItemPosition();
    		
    		// Fetch country
    		spinner = (Spinner)findViewById(R.id.ProfileDetailsCountry);
    		m_usrProfile.m_nCountry = spinner.getSelectedItemPosition();
    		
    		// Fetch town
    		m_usrProfile.m_strTown = ((EditText)findViewById(R.id.ProfileDetailsTown)).getText().toString();
    		
			m_connection.SubmitProfile(m_usrProfile);
    		m_connection.Unregister();
    		this.setResult(1);
    		finish();

    		return true;
		}
		else if (item.getId() == 2)
		{
			finish();
			return true;
		}
		
		return false;
	}

    public class ImageAdapter extends BaseAdapter
    {
        public ImageAdapter(Context c) { m_context = c; }

        public int getCount() { return ProfileUser.idImages.length; }

        public Object getItem(int position) { return position; }

        public long getItemId(int position) { return position; }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(m_context);

            i.setImageResource(ProfileUser.idImages[position]);
            i.setScaleType(ImageView.ScaleType.FIT_XY);
            i.setLayoutParams(new Gallery.LayoutParams(50, 50));
            return i;
        }

        public float getAlpha(boolean focused, int offset) {
            return Math.max(0, 1.0f - (0.2f * Math.abs(offset)));
        }

        public float getScale(boolean focused, int offset) {
            return Math.max(0, 1.0f - (0.2f * Math.abs(offset)));
        }

        private Context m_context;

    }

    public static final int arrInterests[] = {
		R.id.chkboxDetailsEntertainment,
		R.id.chkboxDetailsSportHealth,
		R.id.chkboxDetailsFashion,
		R.id.chkboxDetailsTech,
		R.id.chkboxDetailsCelebrity,
		R.id.chkboxDetailsPolitics,
		R.id.chkboxDetailsMisccelenious
	};
    
}
